﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class MakerDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Maker.GetAll);
		}

		public static DataTable GetOne(string makerId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@MakerID", SqlDbType.NVarChar, makerId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Maker.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static Maker LoadOne(string makerId)
		{
			Maker o = new Maker();
			DataTable dt = GetOne(makerId);
			if(dt.Rows.Count > 0)
			{
				o.MakerID = dt.Rows[0][ToolConstants.Entities.Maker.FieldName.MakerID].ToString();
				o.MakerName = dt.Rows[0][ToolConstants.Entities.Maker.FieldName.MakerName].ToString();
				//o.EnglishName = dt.Rows[0][ToolConstants.Entities.Maker.FieldName.EnglishName].ToString();

			}

			return o;
		}


		public static int Insert(Maker o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@MakerName", SqlDbType.NVarChar, o.MakerName),
                        //CreateParameter("@Note", SqlDbType.NVarChar, o.EnglishName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Maker.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(Maker o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@MakerName", SqlDbType.NVarChar, o.MakerName),
                        //CreateParameter("@Note", SqlDbType.NVarChar, o.EnglishName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Maker.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(Maker o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Maker.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("222_Maker");

			return table;
		}

		#endregion
	}
}
